package com.ml.doctor.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.google.gson.reflect.TypeToken;
import com.ml.doctor.R;
import com.ml.doctor.bean.TXBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.LocalShared;
import com.ml.doctor.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TXMXActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    TXMXAdapter xdAdapter;
    @BindView(R.id.ivArrow)
    ImageView ivArrow;
    @BindView(R.id.ivSuccess)
    ImageView ivSuccess;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.tvRefresh)
    TextView tvRefresh;
    @BindView(R.id.swipe_target)
    ListView swipeTarget;
    @BindView(R.id.swipeToLoadLayout)
    SwipeToLoadLayout swipeToLoadLayout;

    List<TXBean> list;
    int page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_txmx);
        ButterKnife.bind(this);
        setTopTitle("提现明细");
        swipeToLoadLayout.setOnLoadMoreListener(this);
        swipeToLoadLayout.setOnRefreshListener(this);
        list = new ArrayList<>();
        xdAdapter = new TXMXAdapter();
        swipeTarget.setAdapter(xdAdapter);
        swipeToLoadLayout.setRefreshing(true);
    }

    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("limit", 20 + "");
        map.put("state", 0 + "");
        map.put("ws", 1 + "");
        map.put("name", LocalShared.getInstance(this).getUserNick() + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "/record/selRecords_all", map, new TypeToken<List<TXBean>>() {
        }.getType(), new NetworkManager.SuccessCallback<List<TXBean>>() {
            @Override
            public void onSuccess(List<TXBean> response) {
                if (page == 1)
                    list.clear();
                list.addAll(response);
                page++;
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
                xdAdapter.notifyDataSetChanged();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(TXMXActivity.this, message);
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
            }
        });
    }

    @Override
    public void onLoadMore() {
        getListData();

    }

    @Override
    public void onRefresh() {
        page = 1;
        getListData();
    }

    class TXMXAdapter extends BaseAdapter {
        ViewHolder viewhold;

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(TXMXActivity.this).inflate(R.layout.item_txmx, parent, false);
                viewhold = new ViewHolder(convertView);
                convertView.setTag(viewhold);
            }
            viewhold = (ViewHolder) convertView.getTag();
            viewhold.txtTime.setText(list.get(position).time);
            viewhold.txtGold.setText(list.get(position).amount + "");
            viewhold.txtBz.setText(list.get(position).remark);
            viewhold.txtStatus.setText(list.get(position).state);
            return convertView;
        }

        class ViewHolder {
            @BindView(R.id.txt_time)
            TextView txtTime;
            @BindView(R.id.txt_gold)
            TextView txtGold;
            @BindView(R.id.txt_bz)
            TextView txtBz;
            @BindView(R.id.txt_status)
            TextView txtStatus;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
